package com.aaa.tomcat;

import cn.hutool.core.collection.SpliteratorUtil;
import cn.hutool.core.util.XmlUtil;
import org.reflections.Reflections;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.xpath.XPathConstants;
import java.util.*;

/**
 * @author ：Teacher陈
 * @date ：Created in 2021/1/20 14:59
 * @description：我自己的xml工具类
 * @modified By：
 * @version: 1.0
 */
public class MylUtil {


    /**
     * @create by: Teacher陈
     * @description: 读取web.xml文件
     * @create time: 2021/1/20 16:41
     * @return map
     */
    public static Map<String, String> loadWebXml() {
        //加载web.xml
        Document docResult = XmlUtil.readXML("web.xml");
        //将Document转换成Element
        Element element = docResult.getDocumentElement();
        //按照元素名称servlet查找所有的元素
        List<Element> elementList = XmlUtil.getElements(element, "servlet");
        //key servletName   value clazz
        Map<String, String> servletMap = new HashMap();
        //遍历servlet元素
        for (Element elementServlet : elementList) {
            //获取单个servlet元素的所有孩子
            NodeList childNodes = elementServlet.getChildNodes();
            List<String> tempList = new ArrayList<String>();
            //遍历所有孩子节点
            for (int i = 0; i < childNodes.getLength(); i++) {
                //去除多余的特殊字符和空格
                String textContent = childNodes.item(i).getTextContent().trim().replace("\\n", "")
                        .replace("\\t", "");
                if (textContent != "" && textContent.length() > 0) {
                    tempList.add(textContent);
                }

            }
            //key servletName   value clazz 封装map集合
            servletMap.put(tempList.get(0), tempList.get(1));
        }
        //key servletName   value url
        Map<String, String> servletMappingMap = new HashMap();
        List<Element> elementListMapping = XmlUtil.getElements(element, "servlet-mapping");
        for (Element elementMapping : elementListMapping) {
            NodeList childNodes = elementMapping.getChildNodes();
            List<String> tempList = new ArrayList<String>();
            for (int i = 0; i < childNodes.getLength(); i++) {
                String textContent = childNodes.item(i).getTextContent().trim().replace("\\n", "")
                        .replace("\\t", "");
                if (textContent != "" && textContent.length() > 0) {
                    tempList.add(textContent);
                }
            }
            servletMappingMap.put(tempList.get(0), tempList.get(1));
        }
        //合并两个map
        Set<String> keySet = servletMappingMap.keySet();
        Map<String, String> finalMap = new HashMap();
        for (String s : keySet) {
            finalMap.put(servletMappingMap.get(s), servletMap.get(s));
        }
        System.out.println(finalMap.toString());
        return finalMap;
    }

    /**
     * @create by: Teacher陈
     * @description: 扫描所有的sevlet注解
     * @create time: 2021/1/20 16:58
     * @param packageName
     * @return
     */
    public static Map<String, String> loadServletAnn(String packageName) {
        if(packageName==null||packageName.equals("")){
            packageName="com.aaa.servlet";
        }
        Reflections reflections= new Reflections(packageName);
        Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(MyWebServlet.class);
        Map<String,String> map = new HashMap<String, String>();
        for (Class<?> aClass : classSet) {
            MyWebServlet annotation = aClass.getAnnotation(MyWebServlet.class);
            map.put(annotation.urlPatterns()[0],aClass.getName());
        }
        return map;
    }
}
